package com.leaf.lowcode.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.utils.RegularInstance;
import com.leaf.lowcode.utils.RsaUtil;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;
import java.util.List;


@Component
public class DaoYiYunApiV2 {
    //@Scheduled(initialDelay =  10, fixedRate = 10)
    public void execute() throws Exception {

        System.out.println("==================启动=================");

//    public static void main(String[] args) throws Exception {
        String publicKey = new RsaUtil("rsapublic.xml").Key();

      //  System.out.println(publicKey);

        String msg=" {\n" +
                "            \"visitAppointedTimeStart\":\"2021-01-01\",\n" +
                "                \"visitAppointedTimeEnd\":\"2021-12-01\",\n" +
                "            \"visitorUserName\":\"\"\n" +
                "        }";
        EncrypRSA rsa = new EncrypRSA();
        byte[] ciphertextPublic = rsa.encrypt(publicKey, msg, "PUBLIC");
        String json = new String(Base64.getEncoder().encode(ciphertextPublic));

        //String cleartextPublic = rsa.decryptToString(publicKey,  new String(Base64.getEncoder().encode(ciphertextPrivate)),EncrypRSA.PUBLIC);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        // 以表单的方式提交

        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //添加参数developerId
        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("json", json);

        //封装
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(multiValueMap, headers);
        //post请求
        ResponseEntity<String> responseEntity = restTemplate.exchange("https://workuat.spic.com.cn/mgr/visitor/open/getRecord.do?token=MTVhYThmZTQtYjVlOS00YjVjLThjMTQtNmIzYjRlMjQ4MDEw", method, request, String.class);
        //提交
        String body = responseEntity.getBody();

        JSONObject objects = JSON.parseObject(body);
        JSONArray visitinfoList = (JSONArray) ((JSONObject) objects.get("data")).get("data");
        System.out.println(visitinfoList);
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
            if (subDeviceDTOList != null) {
                for (SubDeviceDTO dto : subDeviceDTOList) {
                    String pid = dto.getPid();
                    String sn = dto.getSn();
                    String din = dto.getDin();
                    if (sn.equals("dyyfkgdtsn")) {
                        //  System.out.println("======================门禁卡========================"+readerdoorstatesend.getIndexCode());
                        JSONObject standardData = new JSONObject();
                        standardData.put("reportTs", System.currentTimeMillis());
                        JSONObject profile = new JSONObject();
                        profile.put("modelId", 200589);
                        profile.put("poiCode", "w0717006");
                        standardData.put("profile", profile);
                        WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                        JSONObject properties = new JSONObject();
                        JSONObject propertie = new JSONObject();
                        long l = System.currentTimeMillis();
                        properties.put("visitinfoList",visitinfoList);
                        standardData.put("properties", properties);
                        standardData.put("events", new JSONObject());
                        //standardData.put("properties", "{}");
                        try {
                              System.out.println("上传数据");
                            String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                              System.out.println("发送设备运行时事件数据==" + devRunEvent);
                               System.out.println("--------------------------------开始传输设备运行时事件数据");
                            wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                        } catch (Exception e) {
                            System.out.println("设备运行时事件数据传输出现异常");
                        }
                    }
                }
            }
        }




}
